package org.csu.awesome.util;

/**
 * @author csucoderlee
 * @version 1.0, Nov 19, 2018
 */
public class StringUtil {
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }

        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i))) == false) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasLength(CharSequence src) {
        return (src != null && src.length() > 0);
    }

    /**
     *
     * <pre>
     *     StringUtils.hasText(null) = false
     *     StringUtils.hasText("") = false
     *     StringUtils.hasText(" ") = false
     *     StringUtils.hasText("12345") = true
     *     StringUtils.hasText(" 12345 ") = true
     * </pre>
     *
     * @param str the CharSequence to check (may be <code>null</code>)
     * @return
     */
    public static boolean hasText(CharSequence str) {
        if (!hasLength(str)) {
            return false;
        }

        for (int i = 0; i < str.length(); i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    /**
     * Trim <i>all</i> whitespace from the given string: leading, trailing, and inbetween characters.
     * 去掉给定字符串中,在该字符串开头、结尾、或中间出现的 <i>全部<i/> 的空白字符(空格，TAB和回车)
     *
     * @param str
     * @return string trimmed.
     * @see java.lang.Character#isWhitespace
     */
    public static String trimAll(String str) {
        if (!hasLength(str)) {
            return str;
        }

        StringBuilder sb = new StringBuilder(str);
        int index = 0;
        while (sb.length() > index) {
            if (Character.isWhitespace(sb.charAt(index))) {
                sb.deleteCharAt(index);
            } else {
                index++;
            }
        }
        return sb.toString();
    }

    /**
     * Trim leading whitespace from the given string.
     * 去掉给定字符串开头的空白字符(空格，TAB和回车)
     *
     * @param str
     * @return string trimmed.
     * @see java.lang.Character#isWhitespace
     */
    public static String trimLeading(String str) {
        if (!hasLength(str)) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) {
            sb.deleteCharAt(0);
        }
        return sb.toString();
    }

    public static String trimTrailing(String str) {
        if (!hasLength(str)) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    private StringUtil() {
        throw new UnsupportedOperationException();
    }


    public static void main(String[] args) {

        String str = " ";

        //输出是1 结论是判断一个字符串是否是空字符串，不能仅仅只用下一行的判断条件判定
        if (null != str && str.length() != 0) {
            System.out.println(str.length());
        }

        String str1 = "";
        //空字符串 length为0
        System.out.println(str1.length());


        String str2 = " a"; //TAB
        String str3 = " a"; //空格
        String str4 = "" +
                "a"; // 回车

        System.out.println("----2---- " + trimAll(str2));
        System.out.println("----3---- " + trimAll(str3));
        System.out.println("----4---- " + trimAll(str4));

        String str5 = "  a";
        System.out.println("----5---- " + trimLeading(str5));

        String str6 = "a  ";
        System.out.println(trimTrailing(str6));
    }
}
